/*
 * @(#)MaintCalendarPerferenceAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.calendar.web;

import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.calendar.bean.CalendarPerference;
import com.dcivision.calendar.bean.CalendarRecord;
import com.dcivision.calendar.dao.CalendarPerferenceDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.web.AbstractActionForm;
import com.dcivision.framework.web.AbstractMaintAction;
import com.dcivision.user.dao.UserGroupDAObject;

/**
  MaintCalendarPerferenceAction.java

  This class is for maint calendar perference.

    @author          Tony Chen
    @company         DCIVision Limited
    @creation date   01/12/2003
    @version         $Revision: 1.8 $
*/

public class MaintCalendarPerferenceAction extends AbstractMaintAction {

  public static final String REVISION = "$Revision: 1.8 $";

  public MaintCalendarPerferenceAction() {
    super();
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.calendar.dao.CalendarPerferenceDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.SCHEDULE_PREFERENCE);
  }

  public ActionForward execute(ActionMapping mapping,
			       ActionForm form,
			       HttpServletRequest request,
			       HttpServletResponse response) throws
      ServletException {

    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }
    Connection conn = getConnection(request);
    String opMode = request.getParameter("opMode");
    String navMode = request.getParameter("navMode");
    UserGroupDAObject userGroupDAO = new UserGroupDAObject(this.
        getSessionContainer(request), conn);
    request.setAttribute("allGroupList", userGroupDAO.getPublicGroupList());
    if ( (opMode == null) && (navMode == null)) {
      this.selectForEditRecord(mapping, form, request, response);
      return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
    }
    else {
      return super.execute(mapping, form, request, response);
    }
  }

  /**
   * Insert a record into the CALENDAR_PERFERENCE table
   * @param mapping            ActionMapping
   * @param form               AbstractActionForm
   * @param request            HttpServletRequest
   * @param response           HttpServletResponse
   * @throws                   ApplicationException
   */
  public void insertRecord(ActionMapping mapping,
			   AbstractActionForm form,
			   HttpServletRequest request,
			   HttpServletResponse response) throws ApplicationException {

    MaintCalendarPerferenceForm maintForm =
	(MaintCalendarPerferenceForm) form;
    CalendarPerference calendarPerference =
	(CalendarPerference) maintForm.getFormData();
    CalendarPerferenceDAObject calendarPerferenceDAO =
	(CalendarPerferenceDAObject)super.getMajorDAObject(request);
    //delete b4 update
    calendarPerferenceDAO.deleteByUserRecordID(calendarPerference);
    calendarPerference =
	(CalendarPerference) calendarPerferenceDAO.insertObject(
	calendarPerference);
    this.commit(request);
    maintForm.setFormData(calendarPerference);
    super.selectRecord(mapping, form, request, response);


  }

  /**
   * Select for the edit record
   * @param mapping          ActionMapping
   * @param form             ActionForm
   * @param request          HttpServletRequest
   * @param response         HttpServletResponse
   * @throws                 ApplicationException
   */
  public void selectForEditRecord(ActionMapping mapping,
				  ActionForm form,
				  HttpServletRequest request,
				  HttpServletResponse response) throws ApplicationException {

    SessionContainer sessionCon = this.getSessionContainer(request);
    CalendarPerference calendarPerference = null;
    // Get the User Record.
    CalendarPerferenceDAObject calendarPerferenceDAO =
	(CalendarPerferenceDAObject)super.getMajorDAObject(request);
    calendarPerference =
	(CalendarPerference) calendarPerferenceDAO.
	getByObjectByUserRecordID(sessionCon.getUserRecordID());

    if (calendarPerference == null) {

      calendarPerference = new CalendarPerference();
      calendarPerference.setDefaultView(CalendarRecord.NAV_MODE_LIST_BY_WEEK);
      calendarPerference.setUserRecordID(sessionCon.getUserRecordID());
      calendarPerference.setStartShowHour(new Integer(0));
      calendarPerference.setEndShowHour(new Integer(23));
    }
    ( (MaintCalendarPerferenceForm) form).setFormData(calendarPerference);

  }

}


